package com.itheima.app.manager.pmsManager;

import cn.hutool.core.bean.BeanUtil;
import com.itheima.commons.PageBean;
import com.itheima.commons.R;
import com.itheima.domain.db.cms.CmsPrefrenceAreaProductRelation;
import com.itheima.domain.db.cms.CmsSubjectProductRelation;
import com.itheima.domain.db.pms.*;
import com.itheima.dto.pms.PmsProductDto;
import com.itheima.service.cmsService.CmsPrefrenceAreaProductRelationService;
import com.itheima.service.cmsService.CmsSubjectProductRelationService;
import com.itheima.service.pmsService.*;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class PmsProductManager {

    @DubboReference
    PmsProductService pmsProductService;


    @DubboReference
    PmsMemberPriceService pmsMemberPriceService;

    @DubboReference
    CmsPrefrenceAreaProductRelationService cmsPrefrenceAreaProductRelationService;


    @DubboReference
    PmsProductAttributeValueService pmsProductAttributeValueService;

    @DubboReference
    PmsProductFullReductionServcie pmsProductFullReductionServcie;
    @DubboReference
    PmsProductLadderService pmsProductLadderService;
    @DubboReference
    PmsSkuStockService pmsSkuStockService;
    @DubboReference
    CmsSubjectProductRelationService cmsSubjectProductRelationService;


    //查询商品
    public R getByIdProduct(Integer brandId, String keyword, Integer pageNum, Integer pageSize, Integer productCategoryId, String productSn, Integer publishStatus, Integer verifyStatus) {

        PageBean<PmsProduct> res = pmsProductService.getByIdProduct(brandId, keyword, pageNum, pageSize, productCategoryId, productSn, publishStatus, verifyStatus);

        return R.ok(res);
    }

    //批量上下架商品
    public R publishStatus(Integer publishStatus, Integer[] ids) {
        pmsProductService.publishStatus(publishStatus, ids);
        return R.ok(null);
    }

    //批量设为新品
    public R newStatus(Integer newStatus, Integer[] ids) {
        pmsProductService.newStatus(newStatus, ids);
        return R.ok(null);
    }

    public R recommendStatus(Integer recommendStatus, Integer[] ids) {
        pmsProductService.recommendStatus(recommendStatus, ids);
        return R.ok(null);
    }

    public R saveProduct(PmsProductDto productDto) {

        PmsProduct pmsProduct = new PmsProduct();
        BeanUtil.copyProperties(productDto, pmsProduct);
        pmsProductService.add(pmsProduct);


        List<PmsMemberPrice> productRelationList = productDto.getMemberPriceList();
        if (productRelationList != null) {
            for (PmsMemberPrice memberPrice : productRelationList) {
                pmsMemberPriceService.add(memberPrice);
            }
        }

        List<CmsPrefrenceAreaProductRelation> relationList = productDto.getPrefrenceAreaProductRelationList();
        if (relationList != null) {
            for (CmsPrefrenceAreaProductRelation relation : relationList) {
                cmsPrefrenceAreaProductRelationService.add(relation);
            }
        }


        List<PmsProductAttributeValue> valueList = productDto.getProductAttributeValueList();
        if (valueList != null) {
            for (PmsProductAttributeValue value : valueList) {
                pmsProductAttributeValueService.add(value);
            }
        }


        List<PmsProductFullReduction> productFullReductionList = productDto.getProductFullReductionList();
        if (productFullReductionList != null) {
            for (PmsProductFullReduction relation : productFullReductionList) {
                pmsProductFullReductionServcie.add(relation);
            }
        }


        List<PmsProductLadder> productLadderList = productDto.getProductLadderList();
        if (productLadderList != null) {
            for (PmsProductLadder pmsProductLadder : productLadderList) {
                pmsProductLadderService.add(pmsProductLadder);
            }
        }


        List<PmsSkuStock> stockList = productDto.getSkuStockList();
        if (stockList != null) {
            for (PmsSkuStock pmsSkuStock : stockList) {
                pmsSkuStockService.add(pmsSkuStock);
            }
        }

        List<CmsSubjectProductRelation> subjectList = productDto.getSubjectProductRelationList();
        if (subjectList != null) {
            for (CmsSubjectProductRelation relation : subjectList) {
                cmsSubjectProductRelationService.add(relation);
            }
        }

        return R.ok(null);
    }

    public R simpleList(Integer keyword) {

        List<PmsProduct> pmsProduct = pmsProductService.getSimpleList(keyword);

        return R.ok(pmsProduct);
    }

    public R updateDeleteStatus(Integer deleteStatus, Integer[] ids) {

        pmsProductService.updateDeleteStatus(deleteStatus,ids);

        return R.ok(null);
    }

    public R updateInfoById(Long id) {

        PmsProductDto vo = new PmsProductDto();

        PmsProduct pmsProduct = pmsProductService.getInfoByBrandId(id);
        BeanUtil.copyProperties(pmsProduct,vo);

        List<PmsMemberPrice> pmsMemberPriceList = pmsMemberPriceService.getInfoByBrandId(id);
        vo.setMemberPriceList(pmsMemberPriceList);

        List<CmsPrefrenceAreaProductRelation> prefrenceAreaProductRelationList = cmsPrefrenceAreaProductRelationService.getInfoByBrandId(id);
        vo.setPrefrenceAreaProductRelationList(prefrenceAreaProductRelationList);

        List<PmsProductAttributeValue> productAttributeValueList = pmsProductAttributeValueService.getInfoByBrandId(id);
        vo.setProductAttributeValueList(productAttributeValueList);

        List<PmsProductFullReduction> productFullReductionList =  pmsProductFullReductionServcie.getInfoByBrandId(id);
        vo.setProductFullReductionList(productFullReductionList);

        List<PmsProductLadder> productLadderList = pmsProductLadderService.getInfoByBrandId(id);
        vo.setProductLadderList(productLadderList);

        List<PmsSkuStock> skuStockList = pmsSkuStockService.getInfoByBrandId(id);
        vo.setSkuStockList(skuStockList);

        List<CmsSubjectProductRelation>  subjectProductRelationList = cmsSubjectProductRelationService.getInfoByBrandId(id);
        vo.setSubjectProductRelationList(subjectProductRelationList);

        return R.ok(vo);
    }


    public R updateByid(Long id, PmsProductDto productDto) {

        productDto.setId(id);


        pmsProductService.update(productDto);


        List<PmsMemberPrice> productRelationList = productDto.getMemberPriceList();
        if (productRelationList != null) {
            for (PmsMemberPrice memberPrice : productRelationList) {
                pmsMemberPriceService.update(memberPrice);
            }
        }

        List<CmsPrefrenceAreaProductRelation> relationList = productDto.getPrefrenceAreaProductRelationList();
        if (relationList != null) {
            for (CmsPrefrenceAreaProductRelation relation : relationList) {
                cmsPrefrenceAreaProductRelationService.update(relation);
            }
        }


        List<PmsProductAttributeValue> valueList = productDto.getProductAttributeValueList();
        if (valueList != null) {
            for (PmsProductAttributeValue value : valueList) {
                pmsProductAttributeValueService.update(value);
            }
        }


        List<PmsProductFullReduction> productFullReductionList = productDto.getProductFullReductionList();
        if (productFullReductionList != null) {
            for (PmsProductFullReduction relation : productFullReductionList) {
                pmsProductFullReductionServcie.update(relation);
            }
        }


        List<PmsProductLadder> productLadderList = productDto.getProductLadderList();
        if (productLadderList != null) {
            for (PmsProductLadder pmsProductLadder : productLadderList) {
                pmsProductLadderService.update(pmsProductLadder);
            }
        }


        List<PmsSkuStock> stockList = productDto.getSkuStockList();
        if (stockList != null) {
            for (PmsSkuStock pmsSkuStock : stockList) {
                pmsSkuStockService.update(pmsSkuStock);
            }
        }

        List<CmsSubjectProductRelation> subjectList = productDto.getSubjectProductRelationList();
        if (subjectList != null) {
            for (CmsSubjectProductRelation relation : subjectList) {
                cmsSubjectProductRelationService.update(relation);
            }
        }

        return R.ok(null);
    }
}
